﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using PaperSystem.Models;
using Webdiyer.WebControls.Mvc;

namespace PaperSystem.Controllers
{
    public class StatisticsController : Controller
    {
        //
        // GET: /Statistics/
        QuestionRepository _questionRepository;
        PaperRepository _paperRepository;
        StatisticsRepository _statisticsRepository;
        PublicRepository _publicRepository;
        public StatisticsController()
        {
            _paperRepository = new PaperRepository();
            _questionRepository = new QuestionRepository();
            _statisticsRepository = new StatisticsRepository();
            _publicRepository = new PublicRepository();
        }

        //显示题目的统计信息
        public ActionResult QuestionList(QuestionQuery info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie

            List<StatisticsQuestionList> questionList = _statisticsRepository.GetQuestionList(info);
            ViewData["course"] = _questionRepository.GetCourse();
            ViewData["type"] = _questionRepository.GetType();
            //用于分页
            PagedList<StatisticsQuestionList> plist = new PagedList<StatisticsQuestionList>(questionList, page ?? 1, 30);
            return View("QuestionList", plist);
        }

        //显示试卷的统计信息
        public ActionResult PaperList(PaperQuery info, int? page)
        {
            //验证cookie
            if (Request.Cookies[FormsAuthentication.FormsCookieName] == null)
            {
                TempData["errorMessage"] = "请先登录，再操作";
                return RedirectToAction("Index", "Login");
            }
            else
            {
                string userId = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
                string role = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).UserData;
                if (role != "教师")
                {
                    TempData["errorMessage"] = "用户角色改变，请再登录";
                    return RedirectToAction("Index", "Login");
                }
                ViewData["userName"] = _publicRepository.UserName(userId, role);
            }//end 验证cookie
            List<StatisticsPaperList> paperList = _statisticsRepository.GetPaperList(info);
            ViewData["course"] = _publicRepository.GetCourseDropdownList();
            //用于分页
            PagedList<StatisticsPaperList> plist = new PagedList<StatisticsPaperList>(paperList, page ?? 1, 30);
            return View("PaperList", plist);
        }


    }
}
